package com.monitor.app_monitor.controller;



import com.monitor.app_monitor.entity.ResultInfo;
import com.monitor.app_monitor.entity.TaskDO;
import com.monitor.app_monitor.entity.Webhook;
import com.monitor.app_monitor.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.quartz.JobKey;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin
@Slf4j
@RestController
@RequestMapping("/v1")
public class MonitorController {

    @Autowired
    private TaskService taskService;

    @PostMapping("/send")
    private ResultInfo<?> sendMasg(@RequestBody Webhook webhook) {
        log.info("shiti->{}",webhook.toString());
        String s = taskService.sendMsg(webhook);
        return ResultInfo.ok(s);
    }

    @GetMapping("/getAllJob")
    public ResultInfo<?> getAllJob() throws SchedulerException {

        final List<TaskDO> alljob = this.taskService.alljob();
        return ResultInfo.ok(alljob);
    }

    @GetMapping("/run-job")
    public ResultInfo<?> runJob(@RequestParam String taskName,@RequestParam String group ) throws SchedulerException {
        this.taskService.run(taskName,group);
        return ResultInfo.ok("执行任务");
    }

    @GetMapping("/run-cellect")
    public ResultInfo<?> runCellect(@RequestParam String taskName) throws SchedulerException {
        final List<TaskDO> alljob = this.taskService.alljob();
        alljob.forEach(job -> {
            if ("cellect".equals(job.getJobGroup()) && job.getJobName().contains(taskName)) {

                try {
                    this.taskService.run(job.getJobName(),"cellect");
                } catch (SchedulerException e) {
                    e.printStackTrace();
                }
            }
        });

        return ResultInfo.ok("执行成功");
    }

}
